import {
  Controller,
  Get,
  Post,
  Put,
  Body,
  Param,
  Query,
  UseGuards,
  ParseIntPipe,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiParam,
  ApiQuery,
} from '@nestjs/swagger';
import { OrderService } from './order.service';
import { PaymentService } from '../payment/payment.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { CurrentUser } from '../auth/decorators/current-user.decorator';
import { User } from '../../entities/user.entity';
import { CreateOrderDto } from './dto/create-order.dto';
import { PayOrderDto } from './dto/pay-order.dto';

@ApiTags('订单管理')
@Controller('orders')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
export class OrderController {
  constructor(
    private readonly orderService: OrderService,
    private readonly paymentService: PaymentService,
  ) {}

  /**
   * 创建订单
   */
  @Post()
  @ApiOperation({ summary: '创建订单' })
  async create(
    @Body() createOrderDto: CreateOrderDto,
    @CurrentUser() user: User,
  ) {
    const order = await this.orderService.create(user.id, createOrderDto);
    return {
      code: 200,
      message: '创建成功',
      data: order,
    };
  }

  /**
   * 获取我的订单列表
   */
  @Get('my-orders')
  @ApiOperation({ summary: '获取我的订单' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async getMyOrders(
    @CurrentUser() user: User,
    @Query('page') page: number = 1,
    @Query('limit') limit: number = 10,
  ) {
    const result = await this.orderService.findByUserId(
      user.id,
      Number(page),
      Number(limit),
    );
    return {
      code: 200,
      message: '获取成功',
      data: result,
    };
  }

  /**
   * 获取订单详情
   */
  @Get(':id')
  @ApiOperation({ summary: '获取订单详情' })
  @ApiParam({ name: 'id', description: '订单ID' })
  async findOne(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: User,
  ) {
    const order = await this.orderService.findOne(id, user.id);

    return {
      code: 200,
      message: '获取成功',
      data: order,
    };
  }

  /**
   * 支付订单
   */
  @Post(':id/pay')
  @ApiOperation({ summary: '支付订单' })
  @ApiParam({ name: 'id', description: '订单ID' })
  async payOrder(
    @Param('id', ParseIntPipe) id: number,
    @Body() payOrderDto: PayOrderDto,
    @CurrentUser() user: User,
  ) {
    // 创建支付
    const payment = await this.paymentService.createPayment(
      id,
      user.id,
      payOrderDto.paymentMethod,
      payOrderDto.paymentType,
    );

    return {
      code: 200,
      message: '支付创建成功',
      data: payment,
    };
  }

  /**
   * 取消订单
   */
  @Put(':id/cancel')
  @ApiOperation({ summary: '取消订单' })
  @ApiParam({ name: 'id', description: '订单ID' })
  async cancelOrder(
    @Param('id', ParseIntPipe) id: number,
    @Body('reason') reason: string,
    @CurrentUser() user: User,
  ) {
    await this.orderService.cancel(id, user.id, reason);
    return {
      code: 200,
      message: '取消成功',
    };
  }

  /**
   * 完成订单
   */
  @Put(':id/complete')
  @ApiOperation({ summary: '完成订单' })
  @ApiParam({ name: 'id', description: '订单ID' })
  async completeOrder(@Param('id', ParseIntPipe) id: number) {
    const order = await this.orderService.complete(id);
    return {
      code: 200,
      message: '订单已完成',
      data: order,
    };
  }
}
